import { is_empty, loc, obj_empty } from 'sui';

const getData = (para, url) => {
    if (!para) {
        return [null, null];
    }

    const main = para["d"];
    if (obj_empty(main)) {
        return [null, null];
    }

    let col = null;
    let row = [];

    if (url === "p") {
        col = [
            {name: loc("db_019"), key: 'index', width: 70},
            {name: `${loc("product_44")}1`, key: 'total', width: 70},
            {name: `${loc("product_44")}2`, key: 'reg', width: 70},
            {name: "p", key: 'pval', width: 70},
        ];

        const data = main.data_t;
        const rej = main.data_r;
        const p = main.maindata;

        data.forEach((d, i) => {
            row.push({
                index: i + 1,
                total: d,
                reg: rej[i],
                pval: p[i],
            });
        })
    }
    else if (url === "u") {
        col = [
            {name: loc("db_019"), key: 'index', width: 70},
            {name: `${loc("product_44")}1`, key: 'total', width: 70},
            {name: `${loc("product_44")}2`, key: 'reg', width: 70},
            {name: "u", key: 'pval', width: 70},
        ];

        const data = main.data_t;
        const rej = main.data_r;
        const p = main.maindata;

        data.forEach((d, i) => {
            row.push({
                index: i + 1,
                total: d,
                reg: rej[i],
                pval: p[i],
            });
        })
    }
    else if (url === "c") {
        col = [
            {name: loc("db_019"), key: 'index', width: 70},
            {name: `${loc("product_44")}1`, key: 'total', width: 70},
            {name: "c", key: 'pval', width: 70},
        ];

        const data = main.data_t;
        const p = main.maindata;

        data.forEach((d, i) => {
            row.push({
                index: i + 1,
                total: d,
                pval: p[i],
            });
        })
    }
    else if (url === "np") {
        col = [
            {name: loc("db_019"), key: 'index', width: 70},
            {name: `${loc("product_44")}1`, key: 'total', width: 70},
            {name: "np", key: 'pval', width: 70},
        ];

        const data = main.data_t;
        const p = main.maindata;

        data.forEach((d, i) => {
            row.push({
                index: i + 1,
                total: d,
                pval: p[i],
            });
        })
    }

    return [col, row];
};

const filter_e = (main, opt, mul, ctp=true, yper=false) => {
    if (!main || !opt) {
        return null;
    }

    const { maindata, ucl, lcl, cl } = main["d"];
    const sta = main.sta;

    const std = parseFloat(sta.std);
    const std_2 = 2 * std;
    const std_3 = 3 * std;

    let adjust = {
        "pt_1": [],
        "pt_3": [],
        "pt_5": [],
        "pt_7": [],
    };

    let diff_3 = [];
    let diff_5 = [];
    let diff_7 = [];

    let line = [];
    let ucl_d = [];
    let cl_d = [];
    let lcl_d = [];

    const is_ucl = opt.s_ucl && isNaN(opt.ucl);
    const is_lcl = opt.s_lcl && isNaN(opt.lcl);

    let min = maindata[0];
    let max = maindata[0];

    maindata.forEach((d, i) => {
        const v = [i, d];

        line.push(v);

        min = Math.min(min, d);
        max = Math.max(max, d);

        if (is_ucl) {
            const t = ucl[i];

            max = Math.max(max, t);
            ucl_d.push([i, t]);
        }

        if (is_lcl) {
            const t = lcl[i];

            min = Math.min(min, t);
            lcl_d.push([i, t]);
        }

        if (Math.abs(v.maindata - cl) >= std_3) {
            adjust.pt_1.push(v);
        }

        diff_3.push(v);
        diff_5.push(v);
        diff_7.push(v);

        if (diff_3.length > 3) {
            diff_3.shift();
        }

        if (diff_5.length > 5) {
            diff_5.shift();
        }

        if (diff_7.length > 7) {
            diff_7.shift();
        }

        if (diff_3.length === 3) {
            let count = 0;
            diff_3.forEach(d => {
                if (Math.abs(d[1] - cl) >= std_2) {
                    count += 1;
                }
            });

            if (count >= 2) {
                adjust.pt_3 = adjust.pt_3.concat(diff_3);
                diff_3 = [];
            }
        }

        if (diff_5.length === 5) {
            let count = 0;
            diff_5.forEach(d => {
                if (Math.abs(d[1] - cl) >= std) {
                    count += 1;
                }
            });

            if (count >= 4) {
                adjust.pt_5 = adjust.pt_5.concat(diff_5);
                diff_5 = [];
            }
        }

        if (diff_7.length === 7) {
            let up = 0;
            let down = 0;
            diff_7.forEach(d => {
                if (d[1] - cl >= 0) {
                    up += 1;
                }
                else {
                    down += 1;
                }
            });

            if (up === 7 || down === 7) {
                adjust.pt_7 = adjust.pt_7.concat(diff_7);
                diff_7 = [];
            }
        }
    });

    const end = line.length - 1;

    if (opt.s_ucl && !isNaN(opt.ucl)) {
        const v = opt.ucl / mul;

        ucl_d.push([0, v]);
        ucl_d.push([end, v]);
    }

    if (opt.s_lcl && !isNaN(opt.lcl)) {
        const v = opt.lcl / mul;

        lcl_d.push([0, v]);
        lcl_d.push([end, v]);
    }

    if (opt.s_cl && ctp) {
        let v = 0;
        if (isNaN(opt.cl)) {
            v = cl;
        }
        else {
            v = opt.cl / mul;
        }

        cl_d.push([0, v]);
        cl_d.push([end, v]);
    }

    const cmd = {
        line: [line],
        adj: adjust,
        yper: yper,
    };

    if (!is_empty(cl_d)) {
        cmd["cl"] = cl_d;
    }

    if (!is_empty(ucl_d)) {
        cmd["ucl"] = ucl_d;
    }

    if (!is_empty(lcl_d)) {
        cmd["lcl"] = lcl_d;
    }

    return cmd;
};

export {filter_e, getData};
